/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Action;
import entity.User;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author daz
 */
public class HomeController implements Controller {

    List<Action> listActions = new LinkedList<Action>();

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        User user = new User();
        user.setName("admin");
        putAction(user.getName());
        String now = (new Date()).toString();
        map.put("name", user.getName());
        map.put("now", now);
        map.put("action", listActions);
        return new ModelAndView("home", "map", map);
    }

    private void putAction(String role) {
        listActions = new LinkedList<Action>();
        Action dataAction = new Action("data.htm", "Data Manager");
        Action searchAction = new Action("search.htm", "Search Books");
        Action reservationAction = new Action("reservation.htm", "Reservation Books");
        Action stateAction = new Action("state.htm", "Books State");
        Action borrowAction = new Action("borrow.htm", "Borrows Book");
        Action returnAction = new Action("return.htm", "Return Book");
        listActions.add(dataAction);
        listActions.add(searchAction);
        listActions.add(reservationAction);
        listActions.add(stateAction);
        if (role.equals("admin")) {
            listActions.add(borrowAction);
            listActions.add(returnAction);
        }
    }
}
